package com.htc.dlc.fragment;
import java.util.ArrayList;
import java.util.List;

import com.htc.adapter.NewsAdapter;
import com.htc.common.ActionEvent;
import com.htc.constants.Constants;
import com.htc.controller.NewsModelController;
import com.htc.dlc.DetailNewsActivity;
import com.htc.dlc.R;
import com.htc.dlc.SettingsActivity;
import com.htc.model.dto.NewsModuleDTO;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class FragmentNews extends Fragment implements OnItemClickListener {
	NewsAdapter adapter;
	ArrayList<Object> arrData;
	TextView tv_actionbar_title;
	ListView lvHome;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_news, container,
				false);
		// tv_actionbar_title =
		// (TextView)rootView.findViewById(R.id.tv_actionbar_title);
		// tv_actionbar_title.setText("News");
		lvHome = ((ListView) rootView.findViewById(R.id.lv_home));
		View localView1 = getActivity().getLayoutInflater().inflate(
				R.layout.activity_footer, null);
		this.lvHome.addFooterView(localView1);
		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		arrData = new ArrayList<Object>();
		adapter = new NewsAdapter(getActivity(), R.layout.adapter_news, arrData);
		lvHome.setAdapter(adapter);
		initData1();
		lvHome.setOnItemClickListener(this);
	}

	private void initData1() {

		ActionEvent e = new ActionEvent(this, Constants.GET_NEWS_MODEL1, null,
				null);
		NewsModelController.getInstance().handleViewEvent(e);
	}

	@SuppressWarnings("unchecked")
	public void handleControllerViewEvent(ActionEvent e) {
		switch (e.action) {
		case Constants.GET_NEWS_MODEL1:
			ArrayList<NewsModuleDTO> newArr = (ArrayList<NewsModuleDTO>)e.viewData;
			new VerySlowTask(newArr).execute();
			break;
		default:
			break;
		}
	}

	public class VerySlowTask extends AsyncTask<String, Object, Void> {
		List<NewsModuleDTO> _newArr;

		public VerySlowTask(List<NewsModuleDTO> arr) {
			_newArr = arr;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(String... params) {
			try {
				for (int i = 0; i < _newArr.size(); i++) {
					publishProgress(_newArr.get(i));

				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			arrData.add(values[0]);

		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			adapter.notifyDataSetChanged();
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		//Object o = adapter.getArrObject().get(position);
		try {
			NewsModuleDTO dto = (NewsModuleDTO)lvHome.getItemAtPosition(position);
			Intent intent = new Intent(getActivity(), DetailNewsActivity.class);
			Bundle data = new Bundle();
			SettingsActivity st = new SettingsActivity();
			if (st.flag == 0) {
				data.putString("title", dto.getNewsModuleTitle());
				data.putString("description", dto.getNewsModuleDescription());
			} else if (st.flag == 1) {
				data.putString("title", dto.getNewsModuleTitleAr());
				data.putString("description", dto.getNewsModuleDescriptionAr());
			}
			
			data.putString("image", dto.getNewsModuleImage());
			intent.putExtras(data);
			startActivity(intent);
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}
}
